<template>
  <el-dialog
      v-dialog-drag
      :title="title"
      :visible.sync="visible"
      :width="width"
      :before-close="handleClose"
      :close-on-click-modal="closeOnClickModal"
      :show-close="showClose"
  >
    <slot></slot>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="handleCancel" :disabled="confirmLoading">{{ cancelText }}</el-button>
      <el-button
          size="mini"
          type="primary"
          @click="handleConfirm"
          :loading="confirmLoading"
          :disabled="confirmDisabled"
      >
        {{ confirmText }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'FormDialog',
  props: {
    title: {
      type: String,
      default: '提示'
    },
    width: {
      type: String,
      default: '50%'
    },
    confirmText: {
      type: String,
      default: '确定'
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    closeOnClickModal: {
      type: Boolean,
      default: false
    },
    confirmDisabled: {
      type: Boolean,
      default: false
    },
    beforeClose: Function,
    showClose: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      confirmLoading: false,
      visible: true,
    }
  },
  methods: {
    handleClose(done) {
      if (this.beforeClose) {
        try {
          // 支持异步的 beforeClose
          const result = this.beforeClose(done);
          if (result && typeof result.then === 'function') {
            // 如果 beforeClose 返回 Promise，则等待其完成
            result.then(() => {
              if (typeof done === 'function') {
                done();
              } else {
                this.handleCancel();
              }
            }).catch(error => {
              console.error('FormDialog beforeClose error:', error);
              // 出错时不关闭对话框
            });
            return;
          }
        } catch (error) {
          console.error('FormDialog beforeClose error:', error);
          // 出错时不关闭对话框
          return;
        }
      }
      
      if (typeof done === 'function') {
        done();
      } else {
        this.handleCancel();
      }
    },
    
    handleCancel() {
      this.$emit('cancel');
      // 延迟设置 visible 为 false，确保事件能被正确处理
      this.$nextTick(() => {
        this.visible = false;
      });
    },

    handleConfirm() {
      this.confirmLoading = true;
      
      // 使用 setTimeout 确保 loading 状态能够正确显示
      setTimeout(async () => {
        try {
          // 支持异步的 confirm 事件处理
          await this.$emit('confirm');
        } catch (error) {
          console.error('FormDialog confirm error:', error);
          // 错误时不关闭对话框
          return;
        } finally {
          this.confirmLoading = false;
        }
      }, 0);
    }
  }
}
</script>

<style scoped>
::v-deep .el-dialog__body {
  padding: 20px 10px 0;
}

::v-deep .el-dialog__header {
  padding: 2px 10px 2px;
  background-color: #1989fa;
  color: white;
}

::v-deep .el-dialog__title {
  color: white;
  font-size: 13px;
  font-family: 微软雅黑, serif;
}

::v-deep .el-dialog__headerbtn {
  top: 6px;
}

::v-deep .el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}
</style>